﻿using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;

namespace ReadyApps.Common
{
    class AppConfigurationSettings : IAppSettings
    {
        #region Properties

        public string WikiConnectionStringName
        {
            get { return GetConfigurationSection().WikiDataSettings.ConnectionStringName; }
        }

        public string WikiDataProviderTypeName
        {
            get { return GetConfigurationSection().WikiDataSettings.TypeName; }
        }

        public string WikiDataProviderAssembly
        {
            get { return GetConfigurationSection().WikiDataSettings.Assembly; }
        }

        public string CKEditorBasePath
        {
            get { return GetConfigurationSection().CKEditorBasePath; }
        }

        public TraceComponent EnabledComponents
        {
            get
            {
                ReadyAppsConfigurationSection config = GetConfigurationSection();
                if (config != null)
                {
                    return config.TraceSettings.EnabledComponents;
                }
                else
                {
                    return TraceComponent.None;
                }
            }
        }

        public TraceLevel EnabledLevel
        {
            get
            {
                ReadyAppsConfigurationSection config = GetConfigurationSection();
                if (config != null)
                {
                    return config.TraceSettings.Level;
                }
                else
                {
                    return TraceLevel.Off;
                }
            }
        }

        public IEnumerable<WikiMapping> WikiMappings
        {
            get
            {
                List<WikiMapping> mappings = new List<WikiMapping>();
                foreach (var configMapping in GetConfigurationSection().WikiMappings.Cast<WikiMappingsElement>())
                {
                    mappings.Add(new WikiMapping(configMapping.Name, configMapping.UrlPrefix, configMapping.MapTo));
                }
                return mappings;
            }
        }

        #endregion

        #region Private Methods

        private ReadyAppsConfigurationSection GetConfigurationSection()
        {
            return (ReadyAppsConfigurationSection)ConfigurationManager.GetSection("readyApps");
        }

        #endregion
    }
}
